package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.BaseActivity;
import com.xgkj.diyiketang.activity.GoodClassActivity;
import com.xgkj.diyiketang.activity.GuoxueVideoTwoActivity;
import com.xgkj.diyiketang.activity.MainActivity;
import com.xgkj.diyiketang.activity.MiShiActivity;
import com.xgkj.diyiketang.activity.MoreVideoListActivity;
import com.xgkj.diyiketang.activity.SellerListActivity;
import com.xgkj.diyiketang.activity.VideoDetailActivity;
import com.xgkj.diyiketang.activity.VideoLiveActivity;
import com.xgkj.diyiketang.comparator.VideoComparator;
import com.xgkj.diyiketang.data.AdvertiseInfo;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.FoodInfo;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.data.VideoParentInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.ImageCycleView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrater on 2015/12/17.
 */
public class ListViewAdapter extends BaseAdapter {

    private final String TAG = getClass().getSimpleName();
    private List<VideoParentInfo> mList = new ArrayList<VideoParentInfo>();
    private Context mContext;
    private ImageCycleView adView;
    private List<AdvertiseInfo> adList = new ArrayList<AdvertiseInfo>();
    private List<FoodInfo> foodlist = new ArrayList<FoodInfo>();
    private ArrayList<GridView> gridViewList;
    //    HorizontalScrollView horizontalScrollView;
//    HorizontalLinearLayout horizontalScrollView;
    GridView gridView;
    ViewPager viewPager;
    private TextView bendiMore;
    private RelativeLayout benditop;
    // DisplayMetrics dm;
    private int NUM = 4; // 每行显示个数

    private int LIEWIDTH;//每列宽度
    private int LIE = 10;//列数
    int width;
    MyGridViewAdapter adapter;

    private String areaName;
    private String areaId;
    private TextView mCourse;//精品课程
    private TextView mLive;//精彩直播
    private TextView mEveryday;//每日精彩
    private TextView mParent;//家长专区
    private TextView mVideo;//视频大赛
    private TextView mTimetable;//今日课表
    private TextView mSummary;//本周总结
    private TextView mPrincipal;//校长推荐
    private VideoComparator mVideoComparator;

    private String mTag;
    private String mFlag;

    GridView zhiboGridView;
    private List<EatShowUserInfo> zhiboList = new ArrayList<EatShowUserInfo>();

    public String getAreaId() {
        return areaId;
    }

    public void setAreaId(String areaId) {
        this.areaId = areaId;
    }

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public ListViewAdapter(Context mContext) {
        this.mContext = mContext;
        mVideoComparator = new VideoComparator();

    }

    public void addImageView(List<AdvertiseInfo> list) {
        adList.clear();
        adList.addAll(list);
    }

    public void addFoodList(List<FoodInfo> flist) {
        foodlist.clear();
        foodlist.addAll(flist);

    }

    public void updateData(List<VideoParentInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }


    public void clearData() {
        if (mList != null) {
            mList.clear();
        }

        notifyDataSetChanged();
    }

    public void addZhiboList(List<EatShowUserInfo> zlist) {
        zhiboList.clear();
        zhiboList.addAll(zlist);
    }

    @Override
    public int getCount() {
        if (mList.size() == 0) {
            return 0;
        }
//        else if (adList.size() != 0 && foodlist.size() != 0) {
//            return mList.size() + 2;
//        } else if (adList.size() != 0 || foodlist.size() != 0) {
//            return mList.size() + 2;
//        }
        return mList.size() + 2;
    }

    @Override
    public Object getItem(int position) {
        if (mList == null) {
            return null;
        } else {
            return mList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = LayoutInflater.from(this.mContext).inflate(R.layout.zhibo_lunbo, parent, false);
            adView = (ImageCycleView) convertView.findViewById(R.id.lunbo);
            if (adList == null || adList.size() == 0) {
                adView.setVisibility(View.GONE);
                return convertView;
            }
            ArrayList<String> urlList = new ArrayList<String>();
            for (AdvertiseInfo adInfo : adList) {
                urlList.add(adInfo.getImageUrl());
            }
            adView.setImageResources(urlList, new ImageCycleView.ImageCycleViewListener() {
                @Override
                public void displayImage(String imageURL, ImageView imageView) {
                    ImageCache.getInstance(mContext).loadBitmapToView(imageView, imageURL, new ViewUtil.Size(ViewUtil.getScreenWidth(mContext), ViewUtil.dip2px(mContext, 180)));
                }

                @Override
                public void onImageClick(int position1, View imageView) {

                    AdvertiseInfo adInfo2 = adList.get(position1);
                    String type = adInfo2.getType();
                    if (type != null && type.equals("1")) {
                        //广告
                        // PalLog.e("AD",adIntent.toString());

                        try {
                            Intent adIntent = new Intent(Intent.ACTION_VIEW);
                            adIntent.setData(Uri.parse(adInfo2.getUrl()));
                            mContext.startActivity(adIntent);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else if (type != null && type.equals("2")) {
                        //视频
                        String videoId = adInfo2.getVideoId();
                        String youkuVideoId = adInfo2.getPath();
                        String uId = adInfo2.getUserId();
                        Intent intent3 = null;
                        if (!TextUtils.isEmpty(adInfo2.getUserId())){
                            String isMovie = adInfo2.getIsMoive();
                            if (isMovie.equals("0")){
                                 intent3 = new Intent(mContext, VideoDetailActivity.class);
                            }else {
                                 intent3 = new Intent(mContext, GuoxueVideoTwoActivity.class);
                            }
                            intent3.putExtra("isMovie",isMovie);
                        }
                        intent3.putExtra("videoid", videoId);
                        intent3.putExtra("UID", uId);
                        intent3.putExtra("YOUKUVIDEOID", youkuVideoId);
                        // intent.putExtra("INFO",vinfo);
                        mContext.startActivity(intent3);
                    } else if (type != null && type.equals("3")) {
                        String jumpPath = adInfo2.getPath();
                        //PalLog.e("Path",jumpPath);
                        if (jumpPath != null && jumpPath.equals("11")) {
                            Intent mishiIntent = new Intent(mContext, MiShiActivity.class);
                            mContext.startActivity(mishiIntent);
                        } else if (jumpPath != null && jumpPath.equals("12")) {

                        } else if (jumpPath != null && jumpPath.equals("13")) {
                            Intent xiaoyuanIntent = new Intent(mContext, MainActivity.class);
                            xiaoyuanIntent.putExtra("Flag", 999);
                            mContext.startActivity(xiaoyuanIntent);
                        }
                    } else if (type != null && type.equals("4")) {
                        String tag = EatShowApp.tag;
//                        if (tag.equals("1")){
//                            mContext.startActivity(new Intent(mContext,ZhuanyunMoneyActivity.class));
//                        }else {
//                            mContext.startActivity(new Intent(mContext, ZhunyunSchoolActivity.class));
//                        }
//                        if (mFlag.equals("1")&&mTag.equals("1")){
//                            mContext.startActivity(new Intent(mContext,ZhuanyunMoneyActivity.class));
//                        }else {
//                            mContext.startActivity(new Intent(mContext, ZhunyunSchoolActivity.class));
//                        }

                    } else if (type != null && type.equals("5")) {

                    }

                }
            });
            return convertView;
        } else if (position == 1) {
            convertView = LayoutInflater.from(this.mContext).inflate(R.layout.yushuwai, parent, false);
            mCourse = (TextView) convertView.findViewById(R.id.recommend_course);
            mCourse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, GoodClassActivity.class);
//                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
//                    intent.putExtra("TYPEID", typeid);
//                    intent.putExtra("TITLE",typeName);
                    intent.putExtra("SUBJECT", "0");
                    mContext.startActivity(intent);
                }
            });

            mLive = (TextView) convertView.findViewById(R.id.recommend_live);
            mLive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent mIntent = new Intent(mContext, GoodClassActivity.class);
////                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
////                    intent.putExtra("TYPEID", typeid);
////                    intent.putExtra("TITLE",typeName);
//                    mIntent.putExtra("SUBJECT", "1");
//                    mContext.startActivity(mIntent);
                    Intent intent = new Intent(mContext, MainActivity.class);
                    intent.putExtra("Flag", 678);
                    mContext.startActivity(intent);
                }

            });

            mEveryday = (TextView) convertView.findViewById(R.id.recommend_every);
            mEveryday.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent wIntent = new Intent(mContext, GoodClassActivity.class);
////                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
////                    intent.putExtra("TYPEID", typeid);
////                    intent.putExtra("TITLE",typeName);
//                    wIntent.putExtra("SUBJECT", "2");
//                    mContext.startActivity(wIntent);
                    Intent intent = new Intent(mContext, VideoLiveActivity.class);
                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
                    intent.putExtra("TYPEID", "d74604615a344df3bb4c569a40e16d28");
                    intent.putExtra("TITLE", "每日精彩");
                    mContext.startActivity(intent);
                }
            });

            mParent = (TextView) convertView.findViewById(R.id.recommend_parent);
            mParent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent zIntent = new Intent(mContext, GoodClassActivity.class);
////                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
////                    intent.putExtra("TYPEID", typeid);
////                    intent.putExtra("TITLE",typeName);
//                    zIntent.putExtra("SUBJECT", "3");
//                    mContext.startActivity(zIntent);
                    Intent intent = new Intent(mContext, VideoLiveActivity.class);
                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
                    intent.putExtra("TYPEID", "31d41764f6a24099a04bc1b614289f2c");
                    intent.putExtra("TITLE", "");
                    mContext.startActivity(intent);
                }
            });
            mVideo = (TextView) convertView.findViewById(R.id.recommend_video);
            mVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent intent = new Intent(mContext, GoodClassActivity.class);
////                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
////                    intent.putExtra("TYPEID", typeid);
////                    intent.putExtra("TITLE",typeName);
//                    intent.putExtra("SUBJECT", "0");
//                    mContext.startActivity(intent);
                    Intent intent = new Intent(mContext, MoreVideoListActivity.class);
                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
                    intent.putExtra("TYPEID", "d2c9debdc6cc497e9517ba1c1d9cde8b");
                    intent.putExtra("TITLE", "父母大学");
                    mContext.startActivity(intent);
                }
            });

            mTimetable = (TextView) convertView.findViewById(R.id.recommend_timetable);
            mTimetable.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent mIntent = new Intent(mContext, GoodClassActivity.class);
////                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
////                    intent.putExtra("TYPEID", typeid);
////                    intent.putExtra("TITLE",typeName);
//                    mIntent.putExtra("SUBJECT", "1");
//                    mContext.startActivity(mIntent);
                    Intent intent = new Intent(mContext, VideoLiveActivity.class);
                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
                    intent.putExtra("TYPEID", "a10e3a144fd6412598c18796951386c1");
                    intent.putExtra("TITLE", "今日课表");
                    mContext.startActivity(intent);
                }

            });

            mSummary = (TextView) convertView.findViewById(R.id.recommend_summary);
            mSummary.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent wIntent = new Intent(mContext, GoodClassActivity.class);
////                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
////                    intent.putExtra("TYPEID", typeid);
////                    intent.putExtra("TITLE",typeName);
//                    wIntent.putExtra("SUBJECT", "2");
//                    mContext.startActivity(wIntent);
                    Intent intent = new Intent(mContext, VideoLiveActivity.class);
                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
                    intent.putExtra("TYPEID", "e9fa420b2d774965b80b48727fe3e968");
                    intent.putExtra("TITLE", "本周总结");
                    mContext.startActivity(intent);
                }
            });

            mPrincipal = (TextView) convertView.findViewById(R.id.recommend_princial);
            mPrincipal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent zIntent = new Intent(mContext, GoodClassActivity.class);
////                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
////                    intent.putExtra("TYPEID", typeid);
////                    intent.putExtra("TITLE",typeName);
//                    zIntent.putExtra("SUBJECT", "3");
//                    mContext.startActivity(zIntent);
                    Intent intent = new Intent(mContext, VideoLiveActivity.class);
                    intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
                    intent.putExtra("TYPEID", "fe45e51fee0f4d43998490cb675a9fa4");
                    intent.putExtra("TITLE", "校长推荐");
                    mContext.startActivity(intent);
                }
            });
            return convertView;
        }

//        else if (position == 2) {
//            convertView = LayoutInflater.from(this.mContext).inflate(R.layout.zuoyou_viewpager, parent, false);
//            viewPager = (ViewPager) convertView.findViewById(R.id.viewpager);
//            benditop = (RelativeLayout) convertView.findViewById(R.id.bendi_top);
//            if (foodlist == null || foodlist.size() == 0) {
//                benditop.setVisibility(View.GONE);
//                viewPager.setVisibility(View.GONE);
//                return convertView;
//            }else {
//                benditop.setVisibility(View.VISIBLE);
//                viewPager.setVisibility(View.VISIBLE);
//            }
//            bendiMore = (TextView) convertView.findViewById(R.id.bendi_more);
//            areaName = EatShowSetting.getInstance(mContext).getLocal();
//            areaId = getAreaId();
//            bendiMore.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(mContext, BendiFoodListActivity.class);
//                    intent.putExtra("AreaName", areaName);
//                    mContext.startActivity(intent);
//                }
//            });
//
//
//
//            initPagerView();
//            viewPager.setAdapter(new MyPagerAdapter(gridViewList));
//
//            return convertView;
//        }
        else if (position == 2) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.shouye_zhibolist, parent, false);
            zhiboGridView = (GridView) convertView.findViewById(R.id.zhibo_list);
            benditop = (RelativeLayout) convertView.findViewById(R.id.bendi_top);

            bendiMore = (TextView) convertView.findViewById(R.id.bendi_more);
            bendiMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, MainActivity.class);
                    intent.putExtra("Flag", 678);
                    mContext.startActivity(intent);
                }
            });

            if (zhiboList == null || zhiboList.size() == 0) {
                benditop.setVisibility(View.GONE);
                zhiboGridView.setVisibility(View.GONE);
                return convertView;
            } else {
                benditop.setVisibility(View.VISIBLE);
                zhiboGridView.setVisibility(View.VISIBLE);
                ShouyeLiveAdapter adapter = new ShouyeLiveAdapter(mContext,zhiboList);
                zhiboGridView.setAdapter(adapter);
//                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                        Intent intent = new Intent(mContext, SecondMishiActivity.class);
//                        intent.putExtra("SELECTPOSITION", position + 1);
//                        intent.putExtra("BTYPE", shangjialist.get(position).getTypeName());
//                        mContext.startActivity(intent);
//                    }
//                });
            }
            return convertView;
        }else{


        ViewHolder holder = new ViewHolder();
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(this.mContext).inflate(
                    R.layout.listview_item, null, false);
            holder.allLayout = (LinearLayout) convertView.findViewById(R.id.all_layout);
            holder.vidoeLogo = (ImageView) convertView.findViewById(R.id.videologo);
            holder.textView = (TextView) convertView
                    .findViewById(R.id.listview_item_imageview);
            holder.gridView = (GridView) convertView
                    .findViewById(R.id.listview_item_gridview);
            holder.moreView = (TextView) convertView.findViewById(R.id.more);
            holder.moreLayout = (RelativeLayout) convertView.findViewById(R.id.more_layout);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // if (holder.imageView != null) {
        // holder.imageView.setImageDrawable(mContext.getResources()
        // .getDrawable(R.drawable.e));
        // }
        VideoParentInfo vpInfo = null;
        if (position >= 3) {
            vpInfo = mList.get(position - 3);
        }
        List<VideoInfo> mList = new ArrayList<>();
        if (vpInfo.getList().size() > 4) {
            Collections.sort(vpInfo.getList(), mVideoComparator);
            for (int i = 0; i < 4; i++) {
                mList.add(vpInfo.getList().get(i));
            }
        } else {
            mList = vpInfo.getList();
        }
        List<VideoInfo> gridListData = mList;
        String videologo = vpInfo.getTypeLogo();
        final String typeid = vpInfo.getTypeId();
        final String typeName = vpInfo.getTypeName();
        String types = vpInfo.getType();
        if (types != null && types.equals("1")) {
            ToastUtil.showMessage(mContext, "您所选择的学校暂未上传视频，已为您推荐了热门视频");
        }
        if (videologo != null && videologo.length() > 0) {
            ImageCache.getInstance(mContext).loadBitmapToView(holder.vidoeLogo, videologo, new ViewUtil.Size(ViewUtil.dip2px(mContext, 12), ViewUtil.dip2px(mContext, 14)));
        }
        holder.textView.setText(vpInfo.getTypeName());
        if (ViewUtil.isNull(vpInfo.getTypeName())) {
            holder.moreLayout.setVisibility(View.GONE);
        } else {
            holder.moreLayout.setVisibility(View.VISIBLE);
        }

        if (gridListData == null || gridListData.size() == 0) {
//            holder.moreView.setOnClickListener(null);
//            holder.moreLayout.setVisibility(View.GONE);
            holder.allLayout.setVisibility(View.GONE);
        } else {
            holder.allLayout.setVisibility(View.VISIBLE);
            final String flag=vpInfo.getFlag();
            holder.moreView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if("1".equals(flag)) {
                        Intent intent = new Intent(mContext, VideoLiveActivity.class);
                        intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
                        intent.putExtra("TYPEID", typeid);
                        intent.putExtra("TITLE", typeName);
                        mContext.startActivity(intent);
                    }else {
                        Intent intent = new Intent(mContext, MoreVideoListActivity.class);
                        intent.putExtra(ProtocolConst.MORE_FROM, ProtocolConst.FORM_FIRST);
                        intent.putExtra("TYPEID", typeid);
                        intent.putExtra("TITLE", typeName);
                        mContext.startActivity(intent);
                    }

                }
            });
        }


        if (holder.gridView != null) {
            final VideoAdapter gridViewAdapter = new VideoAdapter(mContext);
            holder.gridView.setAdapter(gridViewAdapter);
            gridViewAdapter.updateData(gridListData);
            /*holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    VideoInfo vinfo = (VideoInfo) gridViewAdapter.getItem(position);
                    String videoId = vinfo.getVideoId();
                    String youkuVideoId = vinfo.getPath();
                    String uId = vinfo.getUserId();
                    String zuozhe = vinfo.getUserName();
                    String fee = vinfo.getFee();
                    String title = vinfo.getVideoTitle();
                    int typeView = vinfo.getViewType();
//                    if(typeView ==2){
//                        Intent gmIntent = new Intent(mContext, GouMaiKeChengActivity.class);
//                        gmIntent.putExtra("ZUOZHE",zuozhe);
//                        gmIntent.putExtra("FEE",fee);
//                        gmIntent.putExtra("TITLE",title);
//                        gmIntent.putExtra("VDID",videoId);
//                        mContext.startActivity(gmIntent);
//                    }else {
                    Intent intent = new Intent(mContext, VideoDetailActivity.class);
                    //intent.putExtra("videoid","XMTQxNjM5MDI3Mg");
                    intent.putExtra("videoid", videoId);
                    intent.putExtra("UID", uId);
                    intent.putExtra("YOUKUVIDEOID", youkuVideoId);
                    intent.putExtra("ZUOZHE", zuozhe);
                    intent.putExtra("FEE", fee);
                    intent.putExtra("TITLE", title);
                    // intent.putExtra("INFO",vinfo);
                    mContext.startActivity(intent);
//                    }


                }
            });*/
        }
        }
        return convertView;
    }

    private ArrayList<GridView> initPagerView() {
        List<ArrayList<FoodInfo>> dataList = new ArrayList<ArrayList<FoodInfo>>();
        ArrayList<FoodInfo> slist = new ArrayList<FoodInfo>();
        for (int i = 0; i < foodlist.size(); i++) {
            slist.add(foodlist.get(i));
            if ((int) (i + 1) % 4 == 0 || i == foodlist.size() - 1) {
                dataList.add(slist);
                slist = new ArrayList<FoodInfo>();
            }
        }
        gridViewList = new ArrayList<GridView>();
        for (int j = 0; j < dataList.size(); j++) {
            GridView gridView = new GridView(mContext);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            gridView.setLayoutParams(params);
            gridView.setNumColumns(2);
            gridView.setBackgroundColor(mContext.getResources().getColor(R.color.white_bg));
            gridView.setHorizontalSpacing(ViewUtil.dip2px(mContext, 10));
            gridView.setVerticalSpacing(ViewUtil.dip2px(mContext, 10));
            final GridAdapter adapter = new GridAdapter();
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    FoodInfo fdinfo = (FoodInfo) adapter.getItem(position);
                    String fdid = fdinfo.getfoodId();
                    Intent intent = new Intent(mContext, SellerListActivity.class);
                    intent.putExtra("FOODID", fdid);
                    intent.putExtra("AREAID", areaId);
                    mContext.startActivity(intent);
                }
            });
            gridView.setAdapter(adapter);
            adapter.updateData(dataList.get(j));

//            gridView.setStretchMode(GridView.STRETCH_SPACING);
            gridViewList.add(gridView);
        }

        return gridViewList;

    }

    private class ViewHolder {
        TextView textView;
        TextView moreView;
        ImageView vidoeLogo;
        GridView gridView;
        RelativeLayout moreLayout;
        LinearLayout allLayout;
    }

    private void setValue() {
        adapter = new MyGridViewAdapter(mContext, LIE);
        adapter.setAreaId(areaId);
        gridView.setAdapter(adapter);
        adapter.updateData(foodlist);
        LayoutParams params = new LayoutParams(adapter.getCount() * LIEWIDTH,
                LayoutParams.WRAP_CONTENT);
        gridView.setLayoutParams(params);
//        gridView.setColumnWidth(LIEWIDTH);
//        gridView.setStretchMode(GridView.NO_STRETCH);
        int count = adapter.getCount();
        gridView.setNumColumns(count);


    }

    private void getScreenDen() {
        // dm = new DisplayMetrics();
        // getWindowManager().getDefaultDisplay().getMetrics(dm);
        DisplayMetrics dm = new DisplayMetrics();
        ((BaseActivity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
//        WindowManager wm = (WindowManager) mContext
//                .getSystemService(Context.WINDOW_SERVICE);
//
//        width = wm.getDefaultDisplay().getWidth();


    }

    class GridChild {
        ImageView foodIcon;
        TextView foodName;
        TextView foodDesc;
    }


    class GridAdapter extends BaseAdapter {
        ArrayList<FoodInfo> list = new ArrayList<FoodInfo>();

        public GridAdapter(ArrayList<FoodInfo> list) {
            this.list = list;
        }

        public GridAdapter() {
        }

        public void updateData(ArrayList<FoodInfo> arrayList) {
            list = arrayList;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            if (list != null && list.size() > 0) {
                return list.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (list != null && list.size() > 0) {
                return list.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PalLog.e(TAG, "list数量：" + list.size());
            GridChild gridChild = new GridChild();
            if (convertView == null || convertView.getTag() == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.bendi_item, parent, false);
                gridChild.foodIcon = (ImageView) convertView.findViewById(R.id.food_icon);
                gridChild.foodName = (TextView) convertView.findViewById(R.id.food_name);
                gridChild.foodDesc = (TextView) convertView.findViewById(R.id.food_desc);
                convertView.setTag(gridChild);
            } else {
                gridChild = (GridChild) convertView.getTag();
            }

            FoodInfo info = null;
            if (position < list.size()) {
                info = list.get(position);
            }
            if (info != null) {
                if (info.getfoodLogo() != null && info.getfoodLogo().length() != 0) {
                    ImageCache.getInstance(mContext).loadBitmapToView(gridChild.foodIcon, info.getfoodLogo(), new ViewUtil.Size(ViewUtil.dip2px(mContext, 60), ViewUtil.dip2px(mContext, 60)));
                }
                gridChild.foodName.setText(info.getfoodName());
            }

            return convertView;


        }
    }

    class MyPagerAdapter extends PagerAdapter {
        private ArrayList<GridView> views = new ArrayList<GridView>();

        public MyPagerAdapter(ArrayList<GridView> l) {
            views = l;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position), 0);
            return views.get(position);
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

}
